<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>异步 Action | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../docs/advanced/AsyncFlow.html" />
    
    
    <link rel="prev" href="../../docs/advanced/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="3.1" data-basepath="../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/advanced/AsyncActions.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="%E5%BC%82%E6%AD%A5-action">&#x5F02;&#x6B65; Action</h1>
<p>&#x5728; <a href="../basics/index.html">&#x57FA;&#x7840;&#x6559;&#x7A0B;</a> &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; todo &#x5E94;&#x7528;&#x3002;&#x5B83;&#x53EA;&#x6709;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#x3002;&#x6BCF;&#x5F53; dispatch action &#x65F6;&#xFF0C;state &#x4F1A;&#x88AB;&#x7ACB;&#x5373;&#x66F4;&#x65B0;&#x3002;</p>
<p>&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5F00;&#x53D1;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#xFF0C;&#x5F02;&#x6B65;&#x7684;&#x5E94;&#x7528;&#x3002;&#x5B83;&#x5C06;&#x4F7F;&#x7528; Reddit API &#x6765;&#x83B7;&#x53D6;&#x5E76;&#x663E;&#x793A;&#x6307;&#x5B9A; subreddit &#x4E0B;&#x7684;&#x5E16;&#x5B50;&#x5217;&#x8868;&#x3002;&#x90A3;&#x4E48; Redux &#x7A76;&#x7ADF;&#x662F;&#x5982;&#x4F55;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x6570;&#x636E;&#x6D41;&#x7684;&#x5462;&#xFF1F;</p>
<h2 id="action">Action</h2>
<p>&#x5F53;&#x8C03;&#x7528;&#x5F02;&#x6B65; API &#x65F6;&#xFF0C;&#x6709;&#x4E24;&#x4E2A;&#x975E;&#x5E38;&#x5173;&#x952E;&#x7684;&#x65F6;&#x523B;&#xFF1A;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x523B;&#xFF0C;&#x548C;&#x63A5;&#x6536;&#x5230;&#x54CD;&#x5E94;&#x7684;&#x65F6;&#x523B;&#xFF08;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x8D85;&#x65F6;&#xFF09;&#x3002;</p>
<p>&#x8FD9;&#x4E24;&#x4E2A;&#x65F6;&#x523B;&#x90FD;&#x53EF;&#x80FD;&#x4F1A;&#x66F4;&#x6539;&#x5E94;&#x7528;&#x7684; state&#xFF1B;&#x4E3A;&#x6B64;&#xFF0C;&#x4F60;&#x9700;&#x8981; dispatch &#x666E;&#x901A;&#x7684;&#x540C;&#x6B65; action&#x3002;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6BCF;&#x4E2A; API &#x8BF7;&#x6C42;&#x90FD;&#x9700;&#x8981; dispatch &#x81F3;&#x5C11;&#x4E09;&#x79CD; action&#xFF1A;</p>
<ul>
<li><p><strong>&#x4E00;&#x79CD;&#x901A;&#x77E5; reducer &#x8BF7;&#x6C42;&#x5F00;&#x59CB;&#x7684; action&#x3002;</strong></p>
<p>&#x5BF9;&#x4E8E;&#x8FD9;&#x79CD; action&#xFF0C;reducer &#x53EF;&#x80FD;&#x4F1A;&#x5207;&#x6362;&#x4E00;&#x4E0B; state &#x4E2D;&#x7684; <code>isFetching</code> &#x6807;&#x8BB0;&#x3002;&#x4EE5;&#x6B64;&#x6765;&#x544A;&#x8BC9; UI &#x6765;&#x663E;&#x793A;&#x52A0;&#x8F7D;&#x754C;&#x9762;&#x3002;</p>
</li>
<li><p><strong>&#x4E00;&#x79CD;&#x901A;&#x77E5; reducer &#x8BF7;&#x6C42;&#x6210;&#x529F;&#x7684; action&#x3002;</strong></p>
<p>&#x5BF9;&#x4E8E;&#x8FD9;&#x79CD; action&#xFF0C;reducer &#x53EF;&#x80FD;&#x4F1A;&#x628A;&#x63A5;&#x6536;&#x5230;&#x7684;&#x65B0;&#x6570;&#x636E;&#x5408;&#x5E76;&#x5230; state &#x4E2D;&#xFF0C;&#x5E76;&#x91CD;&#x7F6E; <code>isFetching</code>&#x3002;UI &#x5219;&#x4F1A;&#x9690;&#x85CF;&#x52A0;&#x8F7D;&#x754C;&#x9762;&#xFF0C;&#x5E76;&#x663E;&#x793A;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x3002;</p>
</li>
<li><p><strong>&#x4E00;&#x79CD;&#x901A;&#x77E5; reducer &#x8BF7;&#x6C42;&#x5931;&#x8D25;&#x7684; action&#x3002;</strong></p>
<p>&#x5BF9;&#x4E8E;&#x8FD9;&#x79CD; action&#xFF0C;reducer &#x53EF;&#x80FD;&#x4F1A;&#x91CD;&#x7F6E; <code>isFetching</code>&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x6709;&#x4E9B; reducer &#x4F1A;&#x4FDD;&#x5B58;&#x8FD9;&#x4E9B;&#x5931;&#x8D25;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x5728; UI &#x91CC;&#x663E;&#x793A;&#x51FA;&#x6765;&#x3002;</p>
</li>
</ul>
<p>&#x4E3A;&#x4E86;&#x533A;&#x5206;&#x8FD9;&#x4E09;&#x79CD; action&#xFF0C;&#x53EF;&#x80FD;&#x5728; action &#x91CC;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x4E13;&#x95E8;&#x7684; <code>status</code> &#x5B57;&#x6BB5;&#x4F5C;&#x4E3A;&#x6807;&#x8BB0;&#x4F4D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS&apos;</span> <span class="token punctuation">}</span>
<span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS&apos;</span><span class="token punctuation">,</span> status<span class="token punctuation">:</span> <span class="token string">&apos;error&apos;</span><span class="token punctuation">,</span> error<span class="token punctuation">:</span> <span class="token string">&apos;Oops&apos;</span> <span class="token punctuation">}</span>
<span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS&apos;</span><span class="token punctuation">,</span> status<span class="token punctuation">:</span> <span class="token string">&apos;success&apos;</span><span class="token punctuation">,</span> response<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
</code></pre>
<p>&#x53C8;&#x6216;&#x8005;&#x4E3A;&#x5B83;&#x4EEC;&#x5B9A;&#x4E49;&#x4E0D;&#x540C;&#x7684; type&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS_REQUEST&apos;</span> <span class="token punctuation">}</span>
<span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS_FAILURE&apos;</span><span class="token punctuation">,</span> error<span class="token punctuation">:</span> <span class="token string">&apos;Oops&apos;</span> <span class="token punctuation">}</span>
<span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">&apos;FETCH_POSTS_SUCCESS&apos;</span><span class="token punctuation">,</span> response<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
</code></pre>
<p>&#x7A76;&#x7ADF;&#x4F7F;&#x7528;&#x5E26;&#x6709;&#x6807;&#x8BB0;&#x4F4D;&#x7684;&#x540C;&#x4E00;&#x4E2A; action&#xFF0C;&#x8FD8;&#x662F;&#x591A;&#x4E2A; action type &#x5462;&#xFF0C;&#x5B8C;&#x5168;&#x53D6;&#x51B3;&#x4E8E;&#x4F60;&#x3002;&#x8FD9;&#x5E94;&#x8BE5;&#x662F;&#x4F60;&#x7684;&#x56E2;&#x961F;&#x5171;&#x540C;&#x8FBE;&#x6210;&#x7684;&#x7EA6;&#x5B9A;&#x3002;&#x4F7F;&#x7528;&#x591A;&#x4E2A; type &#x4F1A;&#x964D;&#x4F4E;&#x72AF;&#x9519;&#x8BEF;&#x7684;&#x673A;&#x7387;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x50CF; <a href="https://github.com/acdlite/redux-actions" target="_blank">redux-actions</a> &#x8FD9;&#x7C7B;&#x7684;&#x8F85;&#x52A9;&#x5E93;&#x6765;&#x751F;&#x6210; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x548C; reducer &#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x5C31;&#x5B8C;&#x5168;&#x4E0D;&#x662F;&#x95EE;&#x9898;&#x4E86;&#x3002;</p>
<p>&#x65E0;&#x8BBA;&#x4F7F;&#x7528;&#x54EA;&#x79CD;&#x7EA6;&#x5B9A;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x5728;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x4E2D;&#x4FDD;&#x6301;&#x7EDF;&#x4E00;&#x3002;<br>&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684; type &#x6765;&#x505A;&#x3002;</p>
<h2 id="%E5%90%8C%E6%AD%A5-action-%E5%88%9B%E5%BB%BA%E5%87%BD%E6%95%B0%EF%BC%88action-creator%EF%BC%89">&#x540C;&#x6B65; Action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#xFF08;Action Creator&#xFF09;</h2>
<p>&#x4E0B;&#x9762;&#x5148;&#x5B9A;&#x4E49;&#x51E0;&#x4E2A;&#x540C;&#x6B65;&#x7684; action &#x7C7B;&#x578B; &#x548C; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x8981;&#x663E;&#x793A;&#x7684; subreddit&#xFF1A;</p>
<h4 id="-actions-js"><code>actions<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">export</span> <span class="token keyword">const</span> SELECT_SUBREDDIT <span class="token operator">=</span> <span class="token string">&apos;SELECT_SUBREDDIT&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">selectSubreddit</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> SELECT_SUBREDDIT<span class="token punctuation">,</span>
    subreddit
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x6309; &quot;&#x5237;&#x65B0;&quot; &#x6309;&#x94AE;&#x6765;&#x66F4;&#x65B0;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">export</span> <span class="token keyword">const</span> INVALIDATE_SUBREDDIT <span class="token operator">=</span> <span class="token string">&apos;INVALIDATE_SUBREDDIT&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">invalidatesubreddit</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> INVALIDATE_SUBREDDIT<span class="token punctuation">,</span>
    subreddit
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x662F;&#x7528;&#x6237;&#x64CD;&#x4F5C;&#x6765;&#x63A7;&#x5236;&#x7684; action&#x3002;&#x4E5F;&#x6709;&#x53E6;&#x5916;&#x4E00;&#x7C7B; action&#xFF0C;&#x662F;&#x7531;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x6765;&#x63A7;&#x5236;&#x3002;&#x540E;&#x9762;&#x4F1A;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#xFF0C;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x6765;&#x5B9A;&#x4E49;&#x5B83;&#x4EEC;&#x3002;</p>
<p>&#x5F53;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x6307;&#x5B9A; subreddit &#x7684;&#x5E16;&#x5B50;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981; dispatch <code>REQUEST_POSTS</code> action&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">export</span> <span class="token keyword">const</span> REQUEST_POSTS <span class="token operator">=</span> <span class="token string">&apos;REQUEST_POSTS&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">requestPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> REQUEST_POSTS<span class="token punctuation">,</span>
    subreddit
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x628A; <code>REQUEST_POSTS</code> &#x548C; <code>SELECT_SUBREDDIT</code> &#x6216; <code>INVALIDATE_SUBREDDIT</code> &#x5206;&#x5F00;&#x5F88;&#x91CD;&#x8981;&#x3002;&#x867D;&#x7136;&#x5B83;&#x4EEC;&#x7684;&#x53D1;&#x751F;&#x6709;&#x5148;&#x540E;&#x987A;&#x5E8F;&#xFF0C;&#x4F46;&#x968F;&#x7740;&#x5E94;&#x7528;&#x53D8;&#x5F97;&#x590D;&#x6742;&#xFF0C;&#x6709;&#x4E9B;&#x7528;&#x6237;&#x64CD;&#x4F5C;&#xFF08;&#x6BD4;&#x5982;&#xFF0C;&#x9884;&#x52A0;&#x8F7D;&#x6700;&#x6D41;&#x884C;&#x7684; subreddit&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#x81EA;&#x52A8;&#x5237;&#x65B0;&#x8FC7;&#x671F;&#x6570;&#x636E;&#xFF09;&#x540E;&#x9700;&#x8981;&#x9A6C;&#x4E0A;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x3002;&#x8DEF;&#x7531;&#x53D8;&#x5316;&#x65F6;&#x4E5F;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#xFF0C;&#x6240;&#x4EE5;&#x4E00;&#x5F00;&#x59CB;&#x5982;&#x679C;&#x628A;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x548C;&#x7279;&#x5B9A;&#x7684; UI &#x4E8B;&#x4EF6;&#x8026;&#x5408;&#x5230;&#x4E00;&#x8D77;&#x662F;&#x4E0D;&#x660E;&#x667A;&#x7684;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x5F53;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A; dispatch <code>RECEIVE_POSTS</code>&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">export</span> <span class="token keyword">const</span> RECEIVE_POSTS <span class="token operator">=</span> <span class="token string">&apos;RECEIVE_POSTS&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">receivePosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">,</span> json<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> RECEIVE_POSTS<span class="token punctuation">,</span>
    subreddit<span class="token punctuation">,</span>
    posts<span class="token punctuation">:</span> json<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>child <span class="token operator">=</span><span class="token operator">&gt;</span> child<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span>
    receivedAt<span class="token punctuation">:</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5C31;&#x662F;&#x73B0;&#x5728;&#x9700;&#x8981;&#x77E5;&#x9053;&#x7684;&#x6240;&#x6709;&#x5185;&#x5BB9;&#x3002;&#x7A0D;&#x540E;&#x4F1A;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x628A; dispatch action &#x4E0E;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7ED3;&#x5408;&#x8D77;&#x6765;&#x3002;</p>
<blockquote>
<h5 id="%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E9%A1%BB%E7%9F%A5">&#x9519;&#x8BEF;&#x5904;&#x7406;&#x987B;&#x77E5;</h5>
<p>&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5931;&#x8D25;&#x65F6;&#x4E5F;&#x9700;&#x8981; dispatch action&#x3002;&#x867D;&#x7136;&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x505A;&#x9519;&#x8BEF;&#x5904;&#x7406;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E2A; <a href="../introduction/Examples.html#real-world">&#x771F;&#x5B9E;&#x573A;&#x666F;&#x7684;&#x6848;&#x4F8B;</a> &#x4F1A;&#x6F14;&#x793A;&#x4E00;&#x79CD;&#x5B9E;&#x73B0;&#x65B9;&#x6848;&#x3002;</p>
</blockquote>
<h2 id="%E8%AE%BE%E8%AE%A1-state-%E7%BB%93%E6%9E%84">&#x8BBE;&#x8BA1; state &#x7ED3;&#x6784;</h2>
<p>&#x5C31;&#x50CF;&#x5728;&#x57FA;&#x7840;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x5728;&#x529F;&#x80FD;&#x5F00;&#x53D1;&#x524D;&#x4F60;&#x9700;&#x8981; <a href="../basics/Reducers.html#designing-the-state-shape">&#x8BBE;&#x8BA1;&#x5E94;&#x7528;&#x7684; state &#x7ED3;&#x6784;</a>&#x3002;&#x5728;&#x5199;&#x5F02;&#x6B65;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x8003;&#x8651;&#x66F4;&#x591A;&#x7684; state&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x8981;&#x4ED4;&#x7EC6;&#x8003;&#x8651;&#x4E00;&#x4E0B;&#x3002;</p>
<p>&#x8FD9;&#x90E8;&#x5206;&#x5185;&#x5BB9;&#x901A;&#x5E38;&#x8BA9;&#x521D;&#x5B66;&#x8005;&#x611F;&#x5230;&#x8FF7;&#x60D1;&#xFF0C;&#x56E0;&#x4E3A;&#x9009;&#x62E9;&#x54EA;&#x4E9B;&#x4FE1;&#x606F;&#x624D;&#x80FD;&#x6E05;&#x6670;&#x5730;&#x63CF;&#x8FF0;&#x5F02;&#x6B65;&#x5E94;&#x7528;&#x7684; state &#x5E76;&#x4E0D;&#x76F4;&#x89C2;&#xFF0C;&#x8FD8;&#x6709;&#x600E;&#x4E48;&#x7528;&#x4E00;&#x4E2A;&#x6811;&#x6765;&#x628A;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x7EC4;&#x7EC7;&#x8D77;&#x6765;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x6700;&#x901A;&#x7528;&#x7684;&#x6848;&#x4F8B;&#x6765;&#x6253;&#x5934;&#xFF1A;&#x5217;&#x8868;&#x3002;Web &#x5E94;&#x7528;&#x7ECF;&#x5E38;&#x9700;&#x8981;&#x5C55;&#x793A;&#x4E00;&#x4E9B;&#x5185;&#x5BB9;&#x7684;&#x5217;&#x8868;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x5E16;&#x5B50;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x670B;&#x53CB;&#x7684;&#x5217;&#x8868;&#x3002;&#x9996;&#x5148;&#x8981;&#x660E;&#x786E;&#x5E94;&#x7528;&#x8981;&#x663E;&#x793A;&#x54EA;&#x4E9B;&#x5217;&#x8868;&#x3002;&#x7136;&#x540E;&#x628A;&#x5B83;&#x4EEC;&#x5206;&#x5F00;&#x50A8;&#x5B58;&#x5728; state &#x4E2D;&#xFF0C;&#x8FD9;&#x6837;&#x4F60;&#x624D;&#x80FD;&#x5BF9;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x505A;&#x7F13;&#x5B58;&#x5E76;&#x4E14;&#x5728;&#x9700;&#x8981;&#x7684;&#x65F6;&#x5019;&#x518D;&#x6B21;&#x8BF7;&#x6C42;&#x66F4;&#x65B0;&#x6570;&#x636E;&#x3002;</p>
<p>&quot;Reddit &#x5934;&#x6761;&quot; &#x5E94;&#x7528;&#x4F1A;&#x957F;&#x8FD9;&#x4E2A;&#x6837;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  selectedsubreddit<span class="token punctuation">:</span> <span class="token string">&apos;frontend&apos;</span><span class="token punctuation">,</span>
  postsBySubreddit<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    frontend<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      isFetching<span class="token punctuation">:</span> <span class="token keyword">true</span><span class="token punctuation">,</span>
      didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      items<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    reactjs<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      isFetching<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      lastUpdated<span class="token punctuation">:</span> <span class="token number">1439478405547</span><span class="token punctuation">,</span>
      items<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          id<span class="token punctuation">:</span> <span class="token number">42</span><span class="token punctuation">,</span>
          title<span class="token punctuation">:</span> <span class="token string">&apos;Confusion about Flux and Relay&apos;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
          id<span class="token punctuation">:</span> <span class="token number">500</span><span class="token punctuation">,</span>
          title<span class="token punctuation">:</span> <span class="token string">&apos;Creating a Simple Application Using React JS and Flux Architecture&apos;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x5217;&#x51FA;&#x51E0;&#x4E2A;&#x8981;&#x70B9;&#xFF1A;</p>
<ul>
<li><p>&#x5206;&#x5F00;&#x5B58;&#x50A8; subreddit &#x4FE1;&#x606F;&#xFF0C;&#x662F;&#x4E3A;&#x4E86;&#x7F13;&#x5B58;&#x6240;&#x6709; subreddit&#x3002;&#x5F53;&#x7528;&#x6237;&#x6765;&#x56DE;&#x5207;&#x6362; subreddit &#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x7ACB;&#x5373;&#x66F4;&#x65B0;&#xFF0C;&#x540C;&#x65F6;&#x5728;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x4E0D;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x3002;&#x4E0D;&#x8981;&#x62C5;&#x5FC3;&#x628A;&#x6240;&#x6709;&#x5E16;&#x5B50;&#x653E;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#xFF08;&#x4F1A;&#x6D6A;&#x8D39;&#x5185;&#x5B58;&#xFF09;&#xFF1A;&#x9664;&#x975E;&#x4F60;&#x9700;&#x8981;&#x5904;&#x7406;&#x6210;&#x5343;&#x4E0A;&#x4E07;&#x6761;&#x5E16;&#x5B50;&#xFF0C;&#x540C;&#x65F6;&#x7528;&#x6237;&#x8FD8;&#x5F88;&#x5C11;&#x5173;&#x95ED;&#x6807;&#x7B7E;&#x9875;&#xFF0C;&#x5426;&#x5219;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x505A;&#x4EFB;&#x4F55;&#x6E05;&#x7406;&#x3002;</p>
</li>
<li><p>&#x6BCF;&#x4E2A;&#x5E16;&#x5B50;&#x7684;&#x5217;&#x8868;&#x90FD;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>isFetching</code> &#x6765;&#x663E;&#x793A;&#x8FDB;&#x5EA6;&#x6761;&#xFF0C;<code>didInvalidate</code> &#x6765;&#x6807;&#x8BB0;&#x6570;&#x636E;&#x662F;&#x5426;&#x8FC7;&#x671F;&#xFF0C;<code>lastUpdated</code> &#x6765;&#x5B58;&#x653E;&#x6570;&#x636E;&#x6700;&#x540E;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF0C;&#x8FD8;&#x6709; <code>items</code> &#x5B58;&#x653E;&#x5217;&#x8868;&#x4FE1;&#x606F;&#x672C;&#x8EAB;&#x3002;&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x4F60;&#x8FD8;&#x9700;&#x8981;&#x5B58;&#x653E; <code>fetchedPageCount</code> &#x548C; <code>nextPageUrl</code> &#x8FD9;&#x6837;&#x5206;&#x9875;&#x76F8;&#x5173;&#x7684; state&#x3002;</p>
</li>
</ul>
<blockquote>
<h5 id="%E5%B5%8C%E5%A5%97%E5%86%85%E5%AE%B9%E9%A1%BB%E7%9F%A5">&#x5D4C;&#x5957;&#x5185;&#x5BB9;&#x987B;&#x77E5;</h5>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x63A5;&#x6536;&#x5230;&#x7684;&#x5217;&#x8868;&#x548C;&#x5206;&#x9875;&#x4FE1;&#x606F;&#x662F;&#x5B58;&#x5728;&#x4E00;&#x8D77;&#x7684;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x5E76;&#x4E0D;&#x9002;&#x7528;&#x4E8E;&#x6709;&#x4E92;&#x76F8;&#x5F15;&#x7528;&#x7684;&#x5D4C;&#x5957;&#x5185;&#x5BB9;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x6216;&#x8005;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x7F16;&#x8F91;&#x5217;&#x8868;&#x7684;&#x573A;&#x666F;&#x3002;&#x60F3;&#x50CF;&#x4E00;&#x4E0B;&#x7528;&#x6237;&#x9700;&#x8981;&#x7F16;&#x8F91;&#x4E00;&#x4E2A;&#x63A5;&#x6536;&#x5230;&#x7684;&#x5E16;&#x5B50;&#xFF0C;&#x4F46;&#x8FD9;&#x4E2A;&#x5E16;&#x5B50;&#x5728; state tree &#x7684;&#x591A;&#x4E2A;&#x4F4D;&#x7F6E;&#x91CD;&#x590D;&#x51FA;&#x73B0;&#x3002;&#x8FD9;&#x4F1A;&#x8BA9;&#x5F00;&#x53D1;&#x53D8;&#x5F97;&#x975E;&#x5E38;&#x56F0;&#x96BE;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x6709;&#x5D4C;&#x5957;&#x5185;&#x5BB9;&#xFF0C;&#x6216;&#x8005;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x7F16;&#x8F91;&#x63A5;&#x6536;&#x5230;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x628A;&#x5B83;&#x4EEC;&#x5206;&#x5F00;&#x5B58;&#x653E;&#x5728; state &#x4E2D;&#xFF0C;&#x5C31;&#x50CF;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x4E00;&#x6837;&#x3002;&#x5728;&#x5206;&#x9875;&#x4FE1;&#x606F;&#x4E2D;&#xFF0C;&#x53EA;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x7684; ID &#x6765;&#x5F15;&#x7528;&#x3002;&#x8FD9;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x59CB;&#x7EC8;&#x4FDD;&#x6301;&#x6570;&#x636E;&#x66F4;&#x65B0;&#x3002;<a href="../introduction/Examples.html#real-world">&#x771F;&#x5B9E;&#x573A;&#x666F;&#x7684;&#x6848;&#x4F8B;</a> &#x4E2D;&#x6F14;&#x793A;&#x4E86;&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#xFF0C;&#x7ED3;&#x5408; <a href="https://github.com/gaearon/normalizr" target="_blank">normalizr</a> &#x6765;&#x628A;&#x5D4C;&#x5957;&#x7684; API &#x54CD;&#x5E94;&#x6570;&#x636E;&#x8303;&#x5F0F;&#x5316;&#xFF0C;&#x6700;&#x7EC8;&#x7684; state &#x770B;&#x8D77;&#x6765;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token punctuation">{</span>
  selectedsubreddit<span class="token punctuation">:</span> <span class="token string">&apos;frontend&apos;</span><span class="token punctuation">,</span>
  entities<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    users<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token number">2</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        name<span class="token punctuation">:</span> <span class="token string">&apos;Andrew&apos;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    posts<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token number">42</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> <span class="token number">42</span><span class="token punctuation">,</span>
        title<span class="token punctuation">:</span> <span class="token string">&apos;Confusion about Flux and Relay&apos;</span><span class="token punctuation">,</span>
        author<span class="token punctuation">:</span> <span class="token number">2</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token number">100</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
        title<span class="token punctuation">:</span> <span class="token string">&apos;Creating a Simple Application Using React JS and Flux Architecture&apos;</span><span class="token punctuation">,</span>
        author<span class="token punctuation">:</span> <span class="token number">2</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  postsBySubreddit<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    frontend<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      isFetching<span class="token punctuation">:</span> <span class="token keyword">true</span><span class="token punctuation">,</span>
      didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      items<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    reactjs<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      isFetching<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
      lastUpdated<span class="token punctuation">:</span> <span class="token number">1439478405547</span><span class="token punctuation">,</span>
      items<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">100</span> <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x5BF9;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x8303;&#x5F0F;&#x5316;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x590D;&#x6742;&#x4E9B;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x3002;</p>
</blockquote>
<h2 id="%E5%A4%84%E7%90%86-action">&#x5904;&#x7406; Action</h2>
<p>&#x5728;&#x8BB2; dispatch action &#x4E0E;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x7EC6;&#x8282;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x4E3A;&#x4E0A;&#x9762;&#x5B9A;&#x4E49;&#x7684; action &#x5F00;&#x53D1;&#x4E00;&#x4E9B; reducer&#x3002;</p>
<blockquote>
<h5 id="reducer-%E7%BB%84%E5%90%88%E9%A1%BB%E7%9F%A5">Reducer &#x7EC4;&#x5408;&#x987B;&#x77E5;</h5>
<p>&#x8FD9;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x4F60;&#x5DF2;&#x7ECF;&#x5B66;&#x4E60;&#x8FC7; <a href="../api/combineReducers.html"><code><span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x5E76;&#x7406;&#x89E3; reducer &#x7EC4;&#x5408;&#xFF0C;&#x8FD8;&#x6709; <a href="../basics/index.html">&#x57FA;&#x7840;&#x7AE0;&#x8282;</a> &#x4E2D;&#x7684; <a href="../basics/Reducers.html#splitting-reducers">&#x62C6;&#x5206; Reducer</a>&#x3002;&#x5982;&#x679C;&#x8FD8;&#x6CA1;&#x6709;&#xFF0C;&#x8BF7; <a href="../basics/Reducers.html#splitting-reducers">&#x5148;&#x5B66;&#x4E60;</a>&#x3002;</p>
</blockquote>
<h4 id="-reducers-js"><code>reducers<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> combineReducers <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span>
  SELECT_SUBREDDIT<span class="token punctuation">,</span> INVALIDATE_SUBREDDIT<span class="token punctuation">,</span>
  REQUEST_POSTS<span class="token punctuation">,</span> RECEIVE_POSTS
<span class="token punctuation">}</span> from <span class="token string">&apos;../actions&apos;</span>

<span class="token keyword">function</span> <span class="token function">selectedsubreddit</span><span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token string">&apos;reactjs&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> SELECT_SUBREDDIT<span class="token punctuation">:</span>
      <span class="token keyword">return</span> action<span class="token punctuation">.</span>subreddit
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> state
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">posts</span><span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token punctuation">{</span>
  isFetching<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
  didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
  items<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> INVALIDATE_SUBREDDIT<span class="token punctuation">:</span>
      <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        didInvalidate<span class="token punctuation">:</span> <span class="token keyword">true</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">case</span> REQUEST_POSTS<span class="token punctuation">:</span>
      <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        isFetching<span class="token punctuation">:</span> <span class="token keyword">true</span><span class="token punctuation">,</span>
        didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">case</span> RECEIVE_POSTS<span class="token punctuation">:</span>
      <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        isFetching<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
        didInvalidate<span class="token punctuation">:</span> <span class="token keyword">false</span><span class="token punctuation">,</span>
        items<span class="token punctuation">:</span> action<span class="token punctuation">.</span>posts<span class="token punctuation">,</span>
        lastUpdated<span class="token punctuation">:</span> action<span class="token punctuation">.</span>receivedAt
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> state
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">postsBySubreddit</span><span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> INVALIDATE_SUBREDDIT<span class="token punctuation">:</span>
    <span class="token keyword">case</span> RECEIVE_POSTS<span class="token punctuation">:</span>
    <span class="token keyword">case</span> REQUEST_POSTS<span class="token punctuation">:</span>
      <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        <span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token function">posts</span><span class="token punctuation">(</span>state<span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> state
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> rootReducer <span class="token operator">=</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  postsBySubreddit<span class="token punctuation">,</span>
  selectedsubreddit
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> rootReducer
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6709;&#x4E24;&#x4E2A;&#x6709;&#x8DA3;&#x7684;&#x70B9;&#xFF1A;</p>
<ul>
<li><p>&#x4F7F;&#x7528; ES6 &#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#x8BED;&#x6CD5;&#xFF0C;&#x4F7F;&#x7528; <code>Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x6765;&#x7B80;&#x6D01;&#x9AD8;&#x6548;&#x5730;&#x66F4;&#x65B0; <code>state<span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span></code>&#x3002;&#x8FD9;&#x4E2A;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token function">posts</span><span class="token punctuation">(</span>state<span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x4E0E;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x7B49;&#x4EF7;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">let</span> nextState <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
nextState<span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">posts</span><span class="token punctuation">(</span>state<span class="token punctuation">[</span>action<span class="token punctuation">.</span>subreddit<span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
<span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> nextState<span class="token punctuation">)</span>
</code></pre>
</li>
<li><p>&#x6211;&#x4EEC;&#x63D0;&#x53D6;&#x51FA; <code><span class="token function">posts</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span></code> &#x6765;&#x7BA1;&#x7406;&#x6307;&#x5B9A;&#x5E16;&#x5B50;&#x5217;&#x8868;&#x7684; state&#x3002;&#x8FD9;&#x4EC5;&#x4EC5;&#x4F7F;&#x7528; <a href="../basics/Reducers.html#splitting-reducers">reducer &#x7EC4;&#x5408;</a> &#x800C;&#x5DF2;&#xFF01;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x501F;&#x6B64;&#x673A;&#x4F1A;&#x628A; reducer &#x5206;&#x62C6;&#x6210;&#x66F4;&#x5C0F;&#x7684; reducer&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x5BF9;&#x8C61;&#x5185;&#x5217;&#x8868;&#x7684;&#x66F4;&#x65B0;&#x4EE3;&#x7406;&#x5230;&#x4E86; <code>posts</code> reducer &#x4E0A;&#x3002;&#x5728; <a href="../introduction/Examples.html#real-world">&#x771F;&#x5B9E;&#x573A;&#x666F;&#x7684;&#x6848;&#x4F8B;</a> &#x4E2D;&#x751A;&#x81F3;&#x66F4;&#x8FDB;&#x4E00;&#x6B65;&#xFF0C;&#x91CC;&#x9762;&#x4ECB;&#x7ECD;&#x4E86;&#x5982;&#x4F55;&#x505A;&#x4E00;&#x4E2A; reducer &#x5DE5;&#x5382;&#x6765;&#x751F;&#x6210;&#x53C2;&#x6570;&#x5316;&#x7684;&#x5206;&#x9875; reducer&#x3002;</p>
</li>
</ul>
<p>&#x8BB0;&#x4F4F; reducer &#x53EA;&#x662F;&#x51FD;&#x6570;&#x800C;&#x5DF2;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x5C3D;&#x60C5;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x7EC4;&#x5408;&#x548C;&#x9AD8;&#x9636;&#x51FD;&#x6570;&#x8FD9;&#x4E9B;&#x7279;&#x6027;&#x3002;</p>
<h2 id="%E5%BC%82%E6%AD%A5-action-%E5%88%9B%E5%BB%BA%E5%87%BD%E6%95%B0">&#x5F02;&#x6B65; action &#x521B;&#x5EFA;&#x51FD;&#x6570;</h2>
<p>&#x6700;&#x540E;&#xFF0C;&#x5982;&#x4F55;&#x628A; <a href="#synchronous-action-creators">&#x4E4B;&#x524D;&#x5B9A;&#x4E49;</a> &#x7684;&#x540C;&#x6B65; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x548C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7ED3;&#x5408;&#x8D77;&#x6765;&#x5462;&#xFF1F;&#x6807;&#x51C6;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x4F7F;&#x7528; <a href="https://github.com/gaearon/redux-thunk" target="_blank">Redux Thunk &#x4E2D;&#x95F4;&#x4EF6;</a>&#x3002;&#x8981;&#x5F15;&#x5165; <code>redux<span class="token operator">-</span>thunk</code> &#x8FD9;&#x4E2A;&#x4E13;&#x95E8;&#x7684;&#x5E93;&#x624D;&#x80FD;&#x4F7F;&#x7528;&#x3002;&#x6211;&#x4EEC; <a href="Middleware.html">&#x540E;&#x9762;</a> &#x4F1A;&#x4ECB;&#x7ECD; middleware &#x5927;&#x4F53;&#x4E0A;&#x662F;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x7684;&#xFF1B;&#x76EE;&#x524D;&#xFF0C;&#x4F60;&#x53EA;&#x9700;&#x8981;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x8981;&#x70B9;&#xFF1A;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x6307;&#x5B9A;&#x7684; middleware&#xFF0C;action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x9664;&#x4E86;&#x8FD4;&#x56DE; action &#x5BF9;&#x8C61;&#x5916;&#x8FD8;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x5C31;&#x6210;&#x4E3A;&#x4E86; <a href="https://en.wikipedia.org/wiki/Thunk" target="_blank">thunk</a>&#x3002;</p>
<p>&#x5F53; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x88AB; Redux Thunk middleware &#x6267;&#x884C;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x4FDD;&#x6301;&#x7EAF;&#x51C0;&#xFF1B;&#x5B83;&#x8FD8;&#x53EF;&#x4EE5;&#x5E26;&#x6709;&#x526F;&#x4F5C;&#x7528;&#xFF0C;&#x5305;&#x62EC;&#x6267;&#x884C;&#x5F02;&#x6B65; API &#x8BF7;&#x6C42;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x8FD8;&#x53EF;&#x4EE5; dispatch action&#xFF0C;&#x5C31;&#x50CF; dispatch &#x524D;&#x9762;&#x5B9A;&#x4E49;&#x7684;&#x540C;&#x6B65; action &#x4E00;&#x6837;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4ECD;&#x53EF;&#x4EE5;&#x5728; <code>actions<span class="token punctuation">.</span>js</code> &#x91CC;&#x5B9A;&#x4E49;&#x8FD9;&#x4E9B;&#x7279;&#x6B8A;&#x7684; thunk action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x3002;</p>
<h4 id="-actions-js"><code>actions<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> fetch from <span class="token string">&apos;isomorphic-fetch&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">const</span> REQUEST_POSTS <span class="token operator">=</span> <span class="token string">&apos;REQUEST_POSTS&apos;</span>
<span class="token keyword">function</span> <span class="token function">requestPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> REQUEST_POSTS<span class="token punctuation">,</span>
    subreddit
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">const</span> RECEIVE_POSTS <span class="token operator">=</span> <span class="token string">&apos;RECEIVE_POSTS&apos;</span>
<span class="token keyword">function</span> <span class="token function">receivePosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">,</span> json<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> RECEIVE_POSTS<span class="token punctuation">,</span>
    subreddit<span class="token punctuation">,</span>
    posts<span class="token punctuation">:</span> json<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>child <span class="token operator">=</span><span class="token operator">&gt;</span> child<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span>
    receivedAt<span class="token punctuation">:</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// &#x6765;&#x770B;&#x4E00;&#x4E0B;&#x6211;&#x4EEC;&#x5199;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; thunk action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#xFF01;</span>
<span class="token comment" spellcheck="true">// &#x867D;&#x7136;&#x5185;&#x90E8;&#x64CD;&#x4F5C;&#x4E0D;&#x540C;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x50CF;&#x5176;&#x5B83; action &#x521B;&#x5EFA;&#x51FD;&#x6570; &#x4E00;&#x6837;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</span>
<span class="token comment" spellcheck="true">// store.dispatch(fetchPosts(&apos;reactjs&apos;))</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">fetchPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment" spellcheck="true">// Thunk middleware &#x77E5;&#x9053;&#x5982;&#x4F55;&#x5904;&#x7406;&#x51FD;&#x6570;&#x3002;</span>
  <span class="token comment" spellcheck="true">// &#x8FD9;&#x91CC;&#x628A; dispatch &#x65B9;&#x6CD5;&#x901A;&#x8FC7;&#x53C2;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x4F20;&#x7ED9;&#x51FD;&#x6570;&#xFF0C;</span>
  <span class="token comment" spellcheck="true">// &#x4EE5;&#x6B64;&#x6765;&#x8BA9;&#x5B83;&#x81EA;&#x5DF1;&#x4E5F;&#x80FD; dispatch action&#x3002;</span>

  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment" spellcheck="true">// &#x9996;&#x6B21; dispatch&#xFF1A;&#x66F4;&#x65B0;&#x5E94;&#x7528;&#x7684; state &#x6765;&#x901A;&#x77E5;</span>
    <span class="token comment" spellcheck="true">// API &#x8BF7;&#x6C42;&#x53D1;&#x8D77;&#x4E86;&#x3002;</span>

    <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">requestPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token comment" spellcheck="true">// thunk middleware &#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;</span>
    <span class="token comment" spellcheck="true">// &#x5B83;&#x4F1A;&#x88AB;&#x5F53;&#x4F5C; dispatch &#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F20;&#x9012;&#x3002;</span>

    <span class="token comment" spellcheck="true">// &#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7B49;&#x5F85;&#x5904;&#x7406;&#x7684; promise&#x3002;</span>
    <span class="token comment" spellcheck="true">// &#x8FD9;&#x5E76;&#x4E0D;&#x662F; redux middleware &#x6240;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x4F46;&#x8FD9;&#x5BF9;&#x4E8E;&#x6211;&#x4EEC;&#x800C;&#x8A00;&#x5F88;&#x65B9;&#x4FBF;&#x3002;</span>

    <span class="token keyword">return</span> <span class="token function">fetch</span><span class="token punctuation">(</span>`http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>subreddit<span class="token punctuation">.</span>com<span class="token operator">/</span>r<span class="token operator">/</span>$<span class="token punctuation">{</span>subreddit<span class="token punctuation">}</span><span class="token punctuation">.</span>json`<span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>response <span class="token operator">=</span><span class="token operator">&gt;</span> response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>json <span class="token operator">=</span><span class="token operator">&gt;</span>

        <span class="token comment" spellcheck="true">// &#x53EF;&#x4EE5;&#x591A;&#x6B21; dispatch&#xFF01;</span>
        <span class="token comment" spellcheck="true">// &#x8FD9;&#x91CC;&#xFF0C;&#x4F7F;&#x7528; API &#x8BF7;&#x6C42;&#x7ED3;&#x679C;&#x6765;&#x66F4;&#x65B0;&#x5E94;&#x7528;&#x7684; state&#x3002;</span>

        <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">receivePosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">,</span> json<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">)</span>

      <span class="token comment" spellcheck="true">// &#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x8FD8;&#x9700;&#x8981;</span>
      <span class="token comment" spellcheck="true">// &#x6355;&#x83B7;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7684;&#x5F02;&#x5E38;&#x3002;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<h5 id="-fetch-%E4%BD%BF%E7%94%A8%E9%A1%BB%E7%9F%A5"><code>fetch</code> &#x4F7F;&#x7528;&#x987B;&#x77E5;</h5>
<p>&#x672C;&#x793A;&#x4F8B;&#x4F7F;&#x7528;&#x4E86; <a href="https://developer.mozilla.org/en/docs/Web/API/Fetch_API" target="_blank"><code>fetch</code> API</a>&#x3002;&#x5B83;&#x662F;&#x66FF;&#x4EE3; <code>XMLHttpRequest</code> &#x7528;&#x6765;&#x53D1;&#x9001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7684;&#x975E;&#x5E38;&#x65B0;&#x7684; API&#x3002;&#x7531;&#x4E8E;&#x76EE;&#x524D;&#x5927;&#x591A;&#x6570;&#x6D4F;&#x89C8;&#x5668;&#x539F;&#x751F;&#x8FD8;&#x4E0D;&#x652F;&#x6301;&#x5B83;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F60;&#x4F7F;&#x7528; <a href="https://github.com/matthew-andrews/isomorphic-fetch" target="_blank"><code>isomorphic<span class="token operator">-</span>fetch</code></a> &#x5E93;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x6BCF;&#x6B21;&#x4F7F;&#x7528; `fetch` &#x524D;&#x90FD;&#x8FD9;&#x6837;&#x8C03;&#x7528;&#x4E00;&#x4E0B;</span>
<span class="token keyword">import</span> fetch from <span class="token string">&apos;isomorphic-fetch&apos;</span>
</code></pre>
<p>&#x5728;&#x5E95;&#x5C42;&#xFF0C;&#x5B83;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x4F7F;&#x7528; <a href="https://github.com/github/fetch" target="_blank"><code>whatwg<span class="token operator">-</span>fetch</code> polyfill</a>&#xFF0C;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x4F7F;&#x7528; <a href="https://github.com/bitinn/node-fetch" target="_blank"><code>node<span class="token operator">-</span>fetch</code></a>&#xFF0C;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x5F53;&#x4F60;&#x628A;&#x5E94;&#x7528;&#x6539;&#x6210; <a href="https://medium.com/@mjackson/universal-javascript-4761051b7ae9" target="_blank">&#x540C;&#x6784;</a> &#x65F6;&#xFF0C;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x6539;&#x53D8; API &#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;<code>fetch</code> polyfill &#x5047;&#x8BBE;&#x4F60;&#x5DF2;&#x7ECF;&#x4F7F;&#x7528;&#x4E86; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" target="_blank">Promise</a> &#x7684; polyfill&#x3002;&#x786E;&#x4FDD;&#x4F60;&#x4F7F;&#x7528; Promise polyfill &#x7684;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x5355;&#x7684;&#x529E;&#x6CD5;&#x662F;&#x5728;&#x6240;&#x6709;&#x5E94;&#x7528;&#x4EE3;&#x7801;&#x524D;&#x542F;&#x7528; Babel &#x7684; ES6 polyfill&#xFF1A;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x5728;&#x5E94;&#x7528;&#x4E2D;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x524D;&#x8C03;&#x7528;&#x4E00;&#x6B21;</span>
<span class="token keyword">import</span> <span class="token string">&apos;babel-polyfill&apos;</span>
</code></pre>
</blockquote>
<p>&#x6211;&#x4EEC;&#x662F;&#x5982;&#x4F55;&#x5728; dispatch &#x673A;&#x5236;&#x4E2D;&#x5F15;&#x5165; Redux Thunk middleware &#x7684;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86; <a href="../api/applyMiddleware.html"><code><span class="token function">applyMiddleware</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a>&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<h4 id="-index-js"><code>index<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> thunkMiddleware from <span class="token string">&apos;redux-thunk&apos;</span>
<span class="token keyword">import</span> createLogger from <span class="token string">&apos;redux-logger&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> createStore<span class="token punctuation">,</span> applyMiddleware <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> selectSubreddit<span class="token punctuation">,</span> fetchPosts <span class="token punctuation">}</span> from <span class="token string">&apos;./actions&apos;</span>
<span class="token keyword">import</span> rootReducer from <span class="token string">&apos;./reducers&apos;</span>

<span class="token keyword">const</span> loggerMiddleware <span class="token operator">=</span> <span class="token function">createLogger</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>
  rootReducer<span class="token punctuation">,</span>
  <span class="token function">applyMiddleware</span><span class="token punctuation">(</span>
    thunkMiddleware<span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// &#x5141;&#x8BB8;&#x6211;&#x4EEC; dispatch() &#x51FD;&#x6570;</span>
    loggerMiddleware <span class="token comment" spellcheck="true">// &#x4E00;&#x4E2A;&#x5F88;&#x4FBF;&#x6377;&#x7684; middleware&#xFF0C;&#x7528;&#x6765;&#x6253;&#x5370; action &#x65E5;&#x5FD7;</span>
  <span class="token punctuation">)</span>
<span class="token punctuation">)</span>

store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">selectSubreddit</span><span class="token punctuation">(</span><span class="token string">&apos;reactjs&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">fetchPosts</span><span class="token punctuation">(</span><span class="token string">&apos;reactjs&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>
<p>thunk &#x7684;&#x4E00;&#x4E2A;&#x4F18;&#x70B9;&#x662F;&#x5B83;&#x7684;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x88AB; dispatch&#xFF1A;</p>
<h4 id="-actions-js"><code>actions<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> fetch from <span class="token string">&apos;isomorphic-fetch&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">const</span> REQUEST_POSTS <span class="token operator">=</span> <span class="token string">&apos;REQUEST_POSTS&apos;</span>
<span class="token keyword">function</span> <span class="token function">requestPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> REQUEST_POSTS<span class="token punctuation">,</span>
    subreddit
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">const</span> RECEIVE_POSTS <span class="token operator">=</span> <span class="token string">&apos;RECEIVE_POSTS&apos;</span>
<span class="token keyword">function</span> <span class="token function">receivePosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">,</span> json<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> RECEIVE_POSTS<span class="token punctuation">,</span>
    subreddit<span class="token punctuation">,</span>
    posts<span class="token punctuation">:</span> json<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>child <span class="token operator">=</span><span class="token operator">&gt;</span> child<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span>
    receivedAt<span class="token punctuation">:</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">fetchPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> dispatch <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">requestPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token function">fetch</span><span class="token punctuation">(</span>`http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>reddit<span class="token punctuation">.</span>com<span class="token operator">/</span>r<span class="token operator">/</span>$<span class="token punctuation">{</span>subreddit<span class="token punctuation">}</span><span class="token punctuation">.</span>json`<span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>response <span class="token operator">=</span><span class="token operator">&gt;</span> response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>json <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">receivePosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">,</span> json<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">shouldFetchPosts</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> posts <span class="token operator">=</span> state<span class="token punctuation">.</span>postsBySubreddit<span class="token punctuation">[</span>subreddit<span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>posts<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">true</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>posts<span class="token punctuation">.</span>isFetching<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">false</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> posts<span class="token punctuation">.</span>didInvalidate
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">fetchPostsIfNeeded</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment" spellcheck="true">// &#x6CE8;&#x610F;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4E5F;&#x63A5;&#x6536;&#x4E86; getState() &#x65B9;&#x6CD5;</span>
  <span class="token comment" spellcheck="true">// &#x5B83;&#x8BA9;&#x4F60;&#x9009;&#x62E9;&#x63A5;&#x4E0B;&#x6765; dispatch &#x4EC0;&#x4E48;&#x3002;</span>

  <span class="token comment" spellcheck="true">// &#x5F53;&#x7F13;&#x5B58;&#x7684;&#x503C;&#x662F;&#x53EF;&#x7528;&#x65F6;&#xFF0C;</span>
  <span class="token comment" spellcheck="true">// &#x51CF;&#x5C11;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5F88;&#x6709;&#x7528;&#x3002;</span>

  <span class="token keyword">return</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">,</span> getState<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">shouldFetchPosts</span><span class="token punctuation">(</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> subreddit<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment" spellcheck="true">// &#x5728; thunk &#x91CC; dispatch &#x53E6;&#x4E00;&#x4E2A; thunk&#xFF01;</span>
      <span class="token keyword">return</span> <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">fetchPosts</span><span class="token punctuation">(</span>subreddit<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token comment" spellcheck="true">// &#x544A;&#x8BC9;&#x8C03;&#x7528;&#x4EE3;&#x7801;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x7B49;&#x5F85;&#x3002;</span>
      <span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x9010;&#x6B65;&#x5F00;&#x53D1;&#x590D;&#x6742;&#x7684;&#x5F02;&#x6B65;&#x63A7;&#x5236;&#x6D41;&#xFF0C;&#x540C;&#x65F6;&#x4FDD;&#x6301;&#x4EE3;&#x7801;&#x6574;&#x6D01;&#x5982;&#x521D;&#xFF1A;</p>
<h4 id="-index-js"><code>index<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js">store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">fetchPostsIfNeeded</span><span class="token punctuation">(</span><span class="token string">&apos;reactjs&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>
<blockquote>
<h5 id="%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93%E9%A1%BB%E7%9F%A5">&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x987B;&#x77E5;</h5>
<p>&#x5F02;&#x6B65; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x5BF9;&#x4E8E;&#x505A;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; store&#xFF0C;dispatch &#x4E00;&#x4E2A;&#x5F02;&#x6B65; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x53C8; dispatch &#x53E6;&#x4E00;&#x4E2A;&#x5F02;&#x6B65; action &#x521B;&#x5EFA;&#x51FD;&#x6570;&#x6765;&#x4E3A;&#x5E94;&#x7528;&#x7684;&#x4E00;&#x6574;&#x5757;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#xFF0C;&#x540C;&#x65F6;&#x5728; Promise &#x5B8C;&#x6210;&#x548C;&#x7ED3;&#x675F;&#x65F6;&#x624D; render &#x754C;&#x9762;&#x3002;&#x7136;&#x540E;&#x5728; render &#x524D;&#xFF0C;store &#x91CC;&#x5C31;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x4E86;&#x9700;&#x8981;&#x7528;&#x7684; state&#x3002;</p>
</blockquote>
<p><a href="https://github.com/gaearon/redux-thunk" target="_blank">Thunk middleware</a> &#x5E76;&#x4E0D;&#x662F; Redux &#x5904;&#x7406;&#x5F02;&#x6B65; action &#x7684;&#x552F;&#x4E00;&#x65B9;&#x5F0F;&#xFF1A;</p>
<ul>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://github.com/acdlite/redux-promise" target="_blank">redux-promise</a> &#x6216;&#x8005; <a href="https://github.com/pburtchaell/redux-promise-middleware" target="_blank">redux-promise-middleware</a> &#x6765; dispatch Promise &#x6765;&#x66FF;&#x4EE3;&#x51FD;&#x6570;&#x3002;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://github.com/redux-observable/redux-observable" target="_blank">redux-observable</a> &#x6765; dispatch Observable&#x3002;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://github.com/yelouafi/redux-saga/" target="_blank">redux-saga</a> &#x4E2D;&#x95F4;&#x4EF6;&#x6765;&#x521B;&#x5EFA;&#x66F4;&#x52A0;&#x590D;&#x6742;&#x7684;&#x5F02;&#x6B65; action&#x3002;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://github.com/lelandrichardson/redux-pack" target="_blank">redux-pack</a> &#x4E2D;&#x95F4;&#x4EF6; dispatch &#x57FA;&#x4E8E; Promise &#x7684;&#x5F02;&#x6B65; Action&#x3002;</li>
<li>&#x4F60;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x5199;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684; middleware &#x6765;&#x63CF;&#x8FF0; API &#x8BF7;&#x6C42;&#xFF0C;&#x5C31;&#x50CF;&#x8FD9;&#x4E2A; <a href="../introduction/Examples.html#real-world">&#x771F;&#x5B9E;&#x573A;&#x666F;&#x7684;&#x6848;&#x4F8B;</a> &#x4E2D;&#x7684;&#x505A;&#x6CD5;&#x4E00;&#x6837;&#x3002;</li>
</ul>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5148;&#x5C1D;&#x8BD5;&#x4E00;&#x4E9B;&#x4E0D;&#x540C;&#x505A;&#x6CD5;&#xFF0C;&#x9009;&#x62E9;&#x559C;&#x6B22;&#x7684;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x4E0B;&#x53BB;&#xFF0C;&#x4E0D;&#x8BBA;&#x6709;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x5230; middleware &#x90FD;&#x884C;&#x3002;</p>
<h2 id="%E8%BF%9E%E6%8E%A5%E5%88%B0-ui">&#x8FDE;&#x63A5;&#x5230; UI</h2>
<p>Dispatch &#x540C;&#x6B65; action &#x4E0E;&#x5F02;&#x6B65; action &#x95F4;&#x5E76;&#x6CA1;&#x6709;&#x533A;&#x522B;&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x4E0D;&#x5C55;&#x5F00;&#x8BA8;&#x8BBA;&#x7EC6;&#x8282;&#x4E86;&#x3002;&#x53C2;&#x7167; <a href="../basics/UsageWithReact.html">&#x642D;&#x914D; React</a> &#x83B7;&#x5F97; React &#x7EC4;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528; Redux &#x7684;&#x4ECB;&#x7ECD;&#x3002;&#x53C2;&#x7167; <a href="ExampleRedditAPI.html">&#x793A;&#x4F8B;&#xFF1A;Reddit API</a> &#x6765;&#x83B7;&#x53D6;&#x672C;&#x4F8B;&#x7684;&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#x3002;</p>
<h2 id="%E4%B8%8B%E4%B8%80%E6%AD%A5">&#x4E0B;&#x4E00;&#x6B65;</h2>
<p>&#x9605;&#x8BFB; <a href="AsyncFlow.html">&#x5F02;&#x6B65;&#x6570;&#x636E;&#x6D41;</a> &#x6765;&#x6574;&#x7406;&#x4E00;&#x4E0B;&#x5F02;&#x6B65; action &#x662F;&#x5982;&#x4F55;&#x9002;&#x7528;&#x4E8E; Redux &#x6570;&#x636E;&#x6D41;&#x7684;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../docs/advanced/index.html" class="navigation navigation-prev " aria-label="Previous page: 高级"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../docs/advanced/AsyncFlow.html" class="navigation navigation-next " aria-label="Next page: 异步数据流"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
